package net.sudot.chess.plugin.payment.ignore;

import net.sudot.chess.business.model.PaymentTransaction;
import net.sudot.chess.plugin.PluginAttributeConfig;
import net.sudot.chess.plugin.payment.PaymentPlugin;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 无须付款的支付插件
 *
 * @author tangjialin on 2018-05-15.
 */
@Component
public class IgnorePaymentPlugin extends PaymentPlugin {
    /** 扩展属性配置 */
    private List<PluginAttributeConfig> attributeConfigs = new ArrayList<>();

    {
        attributeConfigs.add(new PluginAttributeConfig(PaymentPlugin.LOGO_ATTRIBUTE_NAME, "LOGO"));
        attributeConfigs.add(new PluginAttributeConfig(PaymentPlugin.DESCRIPTION_ATTRIBUTE_NAME, "描述"));
    }

    @Override
    public String getName() {
        return "无须付款的支付插件";
    }

    @Override
    public List<PluginAttributeConfig> getAttributeConfigs() {
        return attributeConfigs;
    }

    @Override
    public boolean supports(HttpServletRequest request) {
        return true;
    }

    @Override
    public String payBeforeHandle(PaymentTransaction paymentTransaction, String paymentDescription, String extra, HttpServletRequest request, HttpServletResponse response) {
        return super.payBeforeHandle(paymentTransaction, paymentDescription, extra, request, response);
    }

    @Override
    public String payHandle(PaymentTransaction paymentTransaction, String paymentDescription, String extra, HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("requestUrl", getPayAfterUrl(paymentTransaction.getSn(), extra));
        return super.payHandle(paymentTransaction, paymentDescription, extra, request, response);
    }

    @Override
    public String payAfterHandle(PaymentTransaction paymentTransaction, String paymentDescription, String extra, boolean isPaySuccess, HttpServletRequest request, HttpServletResponse response) {
        return super.payAfterHandle(paymentTransaction, paymentDescription, extra, isPaySuccess, request, response);
    }

    @Override
    public boolean isPaySuccess(PaymentTransaction paymentTransaction, String paymentDescription, String extra, HttpServletRequest request, HttpServletResponse response) {
        return true;
    }

}